15. Exercise: Java Object Serialization

Exercise: Java Object Serialization

Remember the UdacisearchClient "bean" class you used in a previous exercise?

Suppose you need to save a client's information to a file so that it can stick around on a hard drive until the heat death of the universe. How would you do this with Java Serialization?

You are going to write a program to try it out!

  1. Make the UdacisearchClient class extend the java.io.Serializable interface.
  2. Next, in Main.java create an ObjectOutputStream and call ObjectOutputStream#writeObject(client) to serialize the client UdacisearchClient to the outputPath.
  3. Finally, read in the file you just created using ObjectInputStream#readObject() (you'll have to case the result to a UdacisearchClient). If you want, you can print the deserialized object to standard output to make sure it worked.
  4. Now, run the program:
javac Main.java
java Main client.bin

Note: The ".bin" suffix in "client.bin" doesn't actually do anything special; in this example it just makes it so that people looking at the file name will be able to guess that the final contains binary data.

TODO List

Task List:

Task Feedback:

Nice work!

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin